package com.smartHire.controller.User.Address;

import com.smartHire.domain.Address;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DeleteAddressServlet", value = "/DeleteAddressServlet")
public class DeleteAddressServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String addressId = request.getParameter("addressId");
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            UserService userService = (UserService) ctx.getBean("userService");
            com.smartHire.domain.Address address = new Address();
            address.setAddress_Id(Integer.parseInt(addressId));


            if(userService.deleteAddress(address)==1)
            {
                response.getWriter().write("OK");
            }else
            {
                response.getWriter().write("NO");
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
